package com.nokia.hag;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

public class HybridAction {

	private String mode;
	private IWorkbenchWindow window;
	
	public HybridAction(String mode, ExecutionEvent event) throws ExecutionException {
		this.window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		this.mode = mode;
		run();
	}
	
	public void run() {
		String installDir = "";
		
		// Try to read HAG install dir from Win registry
		try {
			Process process = Runtime.getRuntime().exec("reg query \"HKLM\\Software\\Nokia\\Hybrid Application Generator\" /v Install_Dir");
			InputReader reader = new InputReader(process.getInputStream());
			reader.start();
			process.waitFor();
			reader.join();
			String result = reader.getResult();
			String type = "REG_SZ";
			if (result.indexOf(type) > 0) {
				installDir = result.substring(result.indexOf(type) + type.length()).trim();
			}
		} catch (InterruptedException e) {
			MessageDialog.openError(window.getShell(),
					"Error",
					"Interrupted!\n\n" + e.getStackTrace());
		} catch (IOException e) {
			MessageDialog.openError(window.getShell(),
					"Error",
					"IO exception!\n\n" + e.getStackTrace());
		}
		
		if (!installDir.isEmpty()) {
			File directory = new File(installDir);
			File file = new File(installDir + "\\" + "HAG.exe");
			if (file.exists()) {
				try {
					IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					IFile currentOpenFile = (IFile) page.getActiveEditor().getEditorInput().getAdapter(IFile.class);
					
					// We have the file -- try to use it's project path
					if (currentOpenFile != null) {
						String path = currentOpenFile.getProject().getLocation().toString();
						String command = file.getCanonicalPath() + " --mode=" + this.mode + " \"" + path + "\"";
						
						Process process = Runtime.getRuntime().exec(command, null, directory);
						InputReader error = new InputReader(process.getErrorStream());
						InputReader input = new InputReader(process.getInputStream());
						input.start();
						error.start();
					}
				} catch (Exception e) {
					MessageDialog.openError(window.getShell(),
							"Error",
							"Error when launching Hybrid Appication Generator!" + e.toString());
				}
			} else {
				MessageDialog.openError(window.getShell(),
						"Error",
						"Error: could not find HAG.exe in the Hybrid Application generator directory. Please reinstall Hybrid Application Generator.");
			}
		} else {
			MessageDialog.openError(window.getShell(),
					"Error",
					"Error: could not locate HAG installation directory. Please reinstall Hybrid Application Generator.");
		}
	}
}
